package org.example.forum.exception;


import lombok.extern.slf4j.Slf4j;
import org.example.forum.common.AppResult;
import org.example.forum.common.ResultCode;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ResponseBody
    @ExceptionHandler(ApplicationException.class)
    public AppResult applicationExceptionHandler(ApplicationException e) {
        log.error(e.getMessage());
        if(e.errorResult!=null) {
            return e.getErrorResult();
        }
        //非空校验
        if(e.getMessage()==null||e.getMessage().equals("")) {
            return AppResult.failure(ResultCode.ERROR_SERVICES);
        }
        //返回具体的异常信息
        return AppResult.failure(e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public AppResult exceptionHandler(Exception e) {
        log.error(e.getMessage());
        //非空校验
        if(e.getMessage()==null||e.getMessage().equals("")) {
            return AppResult.failure(ResultCode.ERROR_SERVICES);
        }
        //返回具体的异常信息
        return AppResult.failure(e.getMessage());
    }
}
